package com.nightsoul.shiro.test.chapter5.hash.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class MyRealm2 extends AuthorizingRealm {

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		authorizationInfo.addRole("role1");
		return authorizationInfo;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
		String username = "zhang"; //用户名及salt1  
	    String password = "123"; //加密后的密码 ，原密码123
	    String salt2 = "202cb962ac59075b964b07152d234b70";  
	    SimpleAuthenticationInfo ai =   
	        new SimpleAuthenticationInfo(username, password, getName());  
	    		ai.setCredentialsSalt(ByteSource.Util.bytes(username + salt2)); //盐是用户名+随机数  
	        return ai;
	}

}
